#coding=utf-8
from Core import MongoDB


class MongoDBPipeline2(object):

    def open_spider(self, spider):
        #self.database = MongoDB.MongoDB("192.168.1.100", "27017")
        self.database = MongoDB.MongoDB("localhost", "27017")

    def close_spider(self, spider):
        self.database.close()

    def process_item(self, item, spider):

        #self.database.insert("Text","ResearchReport",dict(item))
        if item["Type"] == "ResearchReport":#个股研报
            print("Save To DataBase:" + item["Title"] + ":" + item["DateTime"])
            item["Key"] = item["Title"]+"_" + item["Symbol"] + "_" + item["Issuer"] + "_" + item["DateTime"]
            self.database.upsert("Text","ResearchReport",{"Link":item["Link"]},dict(item))

        elif item["Type"] == "IndustryReport":#行业报告
            print("Save To DataBase:" + item["Title"] + ":" + item["DateTime"])
            item["Key"] = item["Title"] + "_" + item["Issuer"] + "_" + item["DateTime"]
            self.database.upsert("Text","IndustryReport",{"Link":item["Link"]},dict(item))

        elif item["Type"] == "Notice":#个股公告
            print("Save To DataBase:" + item["Title"] + ":" + item["DateTime"])
            item["Key"] = item["Title"]+"_" + item["Symbol"] + "_" + item["DateTime"]
            self.database.upsert("Text","Notice",{"Link":item["Link"]},dict(item))

        elif item["Type"] == "BlockTrade":# 大宗交易
            print("Save BlockTrade To DataBase:" + item["Symbol"] + ":" + item["DateTime"])
            #self.database.insert("Misc","BlockTradeTemp",dict(item))


class MongoDBPipeline(object):

    def __init__(self, mongo_addr, mongo_port):
        self.mongo_addr = mongo_addr
        self.mongo_port = mongo_port


    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            mongo_addr=crawler.settings.get('MONGO_DATABASE_ADDR'),
            mongo_port=crawler.settings.get('MONGO_DATABASE_PORT')
        )


    def open_spider(self, spider):
        self.database = MongoDB.MongoDB(self.mongo_addr, str(self.mongo_port))


    def close_spider(self, spider):
        self.database.close()


    def process_item(self, item, spider):
        if item['DataBaseName'] and item["CollectionName"]:
            databaseName = item['DataBaseName']
            collectionName = item["CollectionName"]

            del item['DataBaseName']
            del item['CollectionName']
            self.database.upsert(databaseName, collectionName, {"Key": item["Key"]}, item)
        else:
            pass
            # raise DropItem("Missing price in %s" % item)

        return item
